package jst.householdmanager.activities;

import java.util.ArrayList;
import java.util.Date;

import jst.householdmanager.GlobalFunc;
import jst.householdmanager.R;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ListEntryPurchaseListPos {

	private Activity activity;

	public ListEntryPurchaseListPos(Activity activity) {
		super();
		this.activity = activity;
	}

	public EntryRecord Record(int id, Date date, String mainText) {
		return new EntryRecord(id, date, mainText, null, null, null);
	}

	public EntryRecord Record(int id, Date date, String mainText, String rightText) {
		return new EntryRecord(id, date, mainText, rightText, null, null);
	}

	public EntryRecord Record(int id, Date date,String mainText, String rightText, String subText1) {
		return new EntryRecord(id, date, mainText, rightText, subText1, null);
	}

	public EntryRecord Record(int id, Date date, String mainText, String rightText, String subText1, String subText2) {
		return new EntryRecord(id, date, mainText, rightText, subText1, subText2);
	}

	public EntryAdapter Adapter(Context context, int textViewResourceId, ArrayList<EntryRecord> listEntries) {
		return new EntryAdapter(context, textViewResourceId, listEntries);
	}

	public class EntryRecord {
		public int id;
		public Date date;
		public String mainText;
		public String rightText;
		public String subText1 = null;
		public String subText2 = null;

		public EntryRecord(int id, Date date, String mainText, String rightText, String subText1, String subText2) {
			super();
			this.id = id;
			this.date = date;
			this.mainText = mainText;
			this.rightText = rightText;
			this.subText1 = subText1;
			this.subText2 = subText2;
		}
	}

	private class EntryAdapter extends ArrayAdapter<EntryRecord> {
		private ArrayList<EntryRecord> listEntries;

		public EntryAdapter(Context context, int textViewResourceId, ArrayList<EntryRecord> listEntries) {
			super(context, textViewResourceId, listEntries);
			this.listEntries = listEntries;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) ListEntryPurchaseListPos.this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.listentry_purchaselistpos, null);
			}

			final EntryRecord listEntry = this.listEntries.get(position);
			if (listEntry != null) {
				TextView maintxt = (TextView) v.findViewById(R.id.listentry_purchaselistpos_maintxt);
				TextView subtxt1 = (TextView) v.findViewById(R.id.listentry_purchaselistpos_subtxt1);
				TextView subtxt2 = (TextView) v.findViewById(R.id.listentry_purchaselistpos_subtxt2);
				TextView righttxt = (TextView) v.findViewById(R.id.listentry_purchaselistpos_righttxt);
				TextView finDate = (TextView) v.findViewById(R.id.listentry_purchaselistpos_findate);
				
				maintxt.setText(listEntry.mainText);

				if ((listEntry.rightText != null) && !listEntry.rightText.matches("")) {
					righttxt.setVisibility(0);
					righttxt.setText(listEntry.rightText);
				}

				if ((listEntry.subText1 != null) && !listEntry.subText1.matches("")) {
					subtxt1.setVisibility(0);
					subtxt1.setText(listEntry.subText1);
				}

				if ((listEntry.subText2 != null) && !listEntry.subText2.matches("")) {
					subtxt2.setVisibility(0);
					subtxt2.setText(listEntry.subText2);
				}

				if (listEntry.date != null) {
					finDate.setVisibility(0);
					finDate.setText(GlobalFunc.DateToString(listEntry.date));
				} 

			}
			return v;
		}
	}
}